package cz.zcu.fav.ratdroid.activity.calendar.reservation;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.LocalDateTime;

import cz.zcu.fav.ratdroid.bean.Period;
import cz.zcu.fav.ratdroid.bean.PickAction;
import cz.zcu.fav.ratdroid.bean.Reservation;
import cz.zcu.fav.ratdroid.bean.Reservations;
import cz.zcu.fav.ratdroid.bean.Object;
import cz.zcu.fav.ratdroid.bean.ReturnAction;

public class ReservationFactory
{

	
	public static void joinReservationToPeriods(Reservations reservations)
	{
		List<Reservation> list = reservations.getReservations();
		for (Reservation res : list)
		{
			for (Period per : res.getPeriods())
			{
				per.setReservation(res);
			}
		}
	}
	
	public static List<Period> getAsPeriodList(Reservations reservations) {
		
		List<Period> periods = new ArrayList<Period>();
		
		List<Reservation> list = reservations.getReservations();
		for (Reservation res : list)
		{
			for (Period per : res.getPeriods())
			{
				per.setReservation(res);
				periods.add(per);
			}
		}
		
		return periods;
	}
	
	
	
	public static Reservations createReservations(LocalDateTime since,
			LocalDateTime to, int weeks, Object obj, int userId, boolean isValid, PickAction pickAction)
	{
		Reservations reservations = new Reservations();
		List<Reservation> reservList = new ArrayList<Reservation>();
		Reservation reservation = new Reservation();
		reservations.setReservations(reservList);
		reservList.add(reservation);
		reservation.setObjectId(obj.getId());
		reservation.setObjectName(obj.getName());
		reservation.setUserId(userId);
		
		List<Period> periodList = new ArrayList<Period>();
		reservation.setPeriods(periodList);

		for (int i = 0; i < weeks; i++)
		{
			
			Period period = new Period();
			period.setFromDate(since);
			period.setToDate(to);
			period.setValid(isValid);
			period.setPickAction(pickAction);
			period.setReturnAction(obj.isReturnable() ? ReturnAction.NOT_RETURNED : ReturnAction.NOT_RETURNABLE);
			periodList.add(period);
			since = since.plusWeeks(1);
			to = to.plusWeeks(1);
		}
		return reservations;
	}
	
	public static Period clonePeriod(Period period) 
	{
		Period newPeriod = new Period();
		newPeriod.setId(period.getId());
		newPeriod.setFromDate(new LocalDateTime(period.getFromDate()));
		newPeriod.setToDate(new LocalDateTime(period.getToDate()));
		newPeriod.setValid(period.isValid());
		newPeriod.setPickAction(period.getPickAction());
		newPeriod.setReturnAction(period.getReturnAction());
		newPeriod.setReservation(period.getReservation());
		return newPeriod;
	}
	
}
